package com.topscomm.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.base.service.IBaseTableExtendService;
import com.topscomm.cbo.controller.auto.CboApprovalFlowControllerAuto;
import com.topscomm.cbo.pojo.CboApprovalFlowInsEntity;
import com.topscomm.cbo.pojo.CboApprovalNodeEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.ICboApprovalFlowInsService;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.cbo.service.ICboApprovalNodeService;
import com.topscomm.cbo.service.ICboUserService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @description:流程通用接口
 * @author: zhanglongfei
 * @date: 2021年1月15日下午6:20:07
 * @modify:
 */
@RestController
@RequestMapping("base/flow")
public class BaseFlowController extends CboApprovalFlowControllerAuto {
	// @Autowired
	// private IBaseFlowRoleInsService baseFlowRoleInsService;
	@Autowired
	private ICboUserService cboUserService;
	@Autowired
	private ICboApprovalNodeInsService cboApprovalNodeInsService;
	@Autowired
	private ICboApprovalFlowInsService cboApprovalFlowInsService;
	@Autowired
	private ICboApprovalNodeService cboApprovalNodeService;
	@Autowired
	private IBaseTableExtendService baseTableExtendService;

	/**
	 * @description:查询审批流下的节点
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年1月15日下午6:22:17
	 * @modify:
	 */
	@GetMapping(value = "/queryApprovaledNode")
	public ResponseResult<Object> queryApprovaledNode(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			List<Map<String, Object>> list = cboApprovalNodeInsService.queryMap(paraMap, "queryApprovaledNode");
			// 处理驳回的节点，排除掉之前的审批记录
			List<Map<String, Object>> resultList = new ArrayList<>();
			boolean needRole = false;
			for (Map<String, Object> map : list) {
				// 如果碰到开始节点，说明之前的是已经退回了
				if ("开始".equals(ConvertUtil.convertToString(map.get("extendfield2")))) {
					resultList.clear();
				}
				// 开始节点不用返回
				String nodeType = ConvertUtil.convertToString(map.get("nodetype"));
				if (!"0".equals(nodeType)) {
					resultList.add(map);
				}
				if ("4".equals(nodeType)) {
					needRole = true;
				}
			}
			// 绑定每个节点是谁审批的(流程角色)
			if (resultList.size() > 0 && needRole) {
				// String flowInsId = ConvertUtil.convertToString(resultList.get(0).get("flowinsid"));
				// List<BaseFlowRoleInsEntity> roleUserList = baseFlowRoleInsService.queryByWhere("flowinsid='" + flowInsId + "'");
				for (Map<String, Object> map : resultList) {
					// 会签节点才需要区分
					if ("4".equals(ConvertUtil.convertToString(map.get("nodetype")))) {
						String userIdString = ConvertUtil.convertToString(map.get("approvaluserid"));
						CboUserEntity userEntity = cboUserService.queryById(userIdString);
						if (userEntity != null) {
							map.put("name", String.format("%s的%s记录", userEntity.getName(), map.get("name")));
						}
						// String planUserExp = ConvertUtil.convertToString(map.get("planuserexp"));
						// if (planUserExp.toLowerCase().indexOf("baseflowroleins") > -1) {
						// // [{"Sql":"select userid from BaseFlowRoleIns where flowinsid={NI:flowinsid} and rolecode='xiaofu'"}]
						// // 获取角色
						//
						// }
					}
				}
			}
			return ResponseResult.ok(resultList);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description:查询当前可以自定义退回的节点
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年3月15日下午7:20:44
	 * @modify:
	 */
	@GetMapping("/queryCustomizeBackNode")
	public ResponseResult<Object> queryCustomizeBackNode(@RequestParam Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		String flowInsId = ConvertUtil.convertToString(paraMap.get("flowinsid"));
		CboApprovalFlowInsEntity flowInsEntity = cboApprovalFlowInsService.queryById(flowInsId);
		CboApprovalNodeEntity nodeEntity = cboApprovalNodeService.queryById(flowInsEntity.getLatestnodeid() + "");
		Map<String, Object> search = new HashMap<>();
		search.put("flowid", nodeEntity.getFlowid());
		search.put("flowinsid", flowInsId);
		search.put("sortcode", nodeEntity.getSortcode());
		try {
			List<Map<String, Object>> list = cboApprovalNodeInsService.queryMap(search, "queryCustomizeBackNode");
			return ResponseResult.ok(list);
		} catch (Exception e) {
			return handleControllerException(e);
		}
	}

	/**
	 * @description:自定义退回
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年3月16日上午11:21:44
	 * @modify:
	 */
	@PostMapping("/customizeBack")
	public ResponseResult<Object> customizeBack(@RequestBody Map<String, Object> paraMap) {
		try {
			baseTableExtendService.customizeBack(paraMap);
			return ResponseResult.ok("操作成功");
		} catch (Exception e) {
			return handleControllerException(e);
			// TODO: handle exception
		}
	}

	/**
	 * @description:获取当前审批节点的信息
	 * @param paraMap
	 * @return
	 * @author: zhanglongfei
	 * @date: 2021年1月19日下午3:18:15
	 * @modify:
	 */
	@GetMapping("/queryNodeInfo")
	public ResponseResult<Object> queryNodeInfo(@RequestParam Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		List<Map<String, Object>> list = cboApprovalNodeInsService.queryMap(paraMap, "myApprovalOneNew");
		if (list != null && list.size() > 0) {
			return ResponseResult.ok(list.get(0));
		} else
			return ResponseResult.error("没有找到对应的待审批数据！");
	}

	/**
	 * @description: 移动端查看审批节点信息
	 * @author: chengengwei
	 * @date: 2021-11-09
	 * @modify:
	 */
	@GetMapping("/queryNodeInfoForMobile")
	public ResponseResult<Object> queryNodeInfoForMobile(@RequestParam Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		List<Map<String, Object>> list = this.cboApprovalNodeInsService.queryMap(paraMap, "myApprovalOne");
		if (list != null && list.size() > 0) {
			// 处于审批中
			return ResponseResult.ok(list.get(0));
		} else
			return ResponseResult.error("没有找到对应的待审批数据！");
	}
}
